package com.ruoyi.gis.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.gis.domain.GisLayerProduct;
import com.ruoyi.gis.mapper.GisLayerProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gis.mapper.GisLayerProductGroupMapper;
import com.ruoyi.gis.domain.GisLayerProductGroup;
import com.ruoyi.gis.service.IGisLayerProductGroupService;

/**
 * layer分组Service业务层处理
 *
 * @author rww
 * @date 2023-11-16
 */
@Service
public class GisLayerProductGroupServiceImpl implements IGisLayerProductGroupService
{
    @Autowired
    private GisLayerProductGroupMapper gisLayerProductGroupMapper;
    @Autowired
    private GisLayerProductMapper gisLayerProductMapper;

    /**
     * 查询layer分组
     *
     * @param id layer分组主键
     * @return layer分组
     */
    @Override
    public GisLayerProductGroup selectGisLayerProductGroupById(Long id)
    {
        return gisLayerProductGroupMapper.selectGisLayerProductGroupById(id);
    }

    /**
     * 查询layer分组列表
     *
     * @param gisLayerProductGroup layer分组
     * @return layer分组
     */
    @Override
    public List<GisLayerProductGroup> selectGisLayerProductGroupList(GisLayerProductGroup gisLayerProductGroup)
    {
        return gisLayerProductGroupMapper.selectGisLayerProductGroupList(gisLayerProductGroup);
    }

    /**
     * 新增layer分组
     *
     * @param gisLayerProductGroup layer分组
     * @return 结果
     */
    @Override
    public int insertGisLayerProductGroup(GisLayerProductGroup gisLayerProductGroup)
    {
        gisLayerProductGroup.setCreateTime(DateUtils.getNowDate());
        return gisLayerProductGroupMapper.insertGisLayerProductGroup(gisLayerProductGroup);
    }

    /**
     * 修改layer分组
     *
     * @param gisLayerProductGroup layer分组
     * @return 结果
     */
    @Override
    public int updateGisLayerProductGroup(GisLayerProductGroup gisLayerProductGroup)
    {
        gisLayerProductGroup.setUpdateTime(DateUtils.getNowDate());
        return gisLayerProductGroupMapper.updateGisLayerProductGroup(gisLayerProductGroup);
    }

    /**
     * 批量删除layer分组
     *
     * @param ids 需要删除的layer分组主键
     * @return 结果
     */
    @Override
    public int deleteGisLayerProductGroupByIds(Long[] ids)
    {
        for(int i=0;i<ids.length;i++){
            deleteGisLayerProductGroupById(ids[i]);
        }
        return ids.length;
    }

    /**
     * 删除layer分组信息
     *
     * @param id layer分组主键
     * @return 结果
     */
    @Override
    public int deleteGisLayerProductGroupById(Long id)
    {
        int result = gisLayerProductGroupMapper.deleteGisLayerProductGroupById(id);
        gisLayerProductMapper.deleteGisLayerProductByGroupId(id);
        return result;
    }

    @Override
    public int updateGisLayerProductGroupSort(List<GisLayerProductGroup> groups) {
        for(int i=0;i<groups.size();i++){
            GisLayerProductGroup group = groups.get(i);
            group.setSort(i);
            gisLayerProductGroupMapper.updateGisLayerProductGroup(group);
        }
        return groups.size();
    }

    @Override
    public int updateGisLayerProductGroupChecked(GisLayerProductGroup group) {
        gisLayerProductGroupMapper.updateGisLayerProductGroup(group);
        gisLayerProductMapper.updateGisLayerCheckedByGroup(group.getId(),group.getChecked());
        return 1;
    }

    @Override
    public int updateGisGroupChecked(GisLayerProductGroup group) {
        Long userId = 0L;
        GisLayerProductGroup query = new GisLayerProductGroup();
        query.setId(group.getId());
        List<GisLayerProductGroup> groupList = gisLayerProductGroupMapper.selectGisLayerProductGroupList(query);
        this.updateDieDaiChecked(groupList,group);

        //gisLayerProductGroupMapper.updateGisLayerProductGroup(group);
        //gisLayerProductMapper.updateGisLayerCheckedByGroup(group.getId(),group.getChecked());
        return 1;
    }

    private void updateDieDaiChecked(List<GisLayerProductGroup> groupList,GisLayerProductGroup group) {
        List<GisLayerProductGroup> resultList = new ArrayList<>();
        if(groupList!=null&&groupList.size()>0){
            groupList.stream().forEach(item->{
                //更改当前组下的图层信息
                gisLayerProductMapper.updateGisLayerCheckedByGroup(item.getId(),group.getChecked());

                //更改当前组信息
                item.setChecked(group.getChecked());
                gisLayerProductGroupMapper.updateGisLayerProductGroup(item);

                //更改当前组下的组信息
                GisLayerProductGroup groupQuery = new GisLayerProductGroup();
                groupQuery.setGroupPid(item.getId());
                List<GisLayerProductGroup> groupTempList = gisLayerProductGroupMapper.selectGisLayerProductGroupList(groupQuery);
                this.updateDieDaiChecked(groupTempList,group);

            });
            resultList.addAll(groupList);
        }
    }


    /**
     * 查询layer分组(迭代查询)
     *
     *
     * @param groupQuery 父级组id=0
     * @return layer分组
     */
    @Override
    public List<GisLayerProductGroup> selectGisLayerProductGroupByPidList(GisLayerProductGroup groupQuery)
    {
        Long userId = groupQuery.getUserId();
        //查询第0级组数据
        List<GisLayerProductGroup> groupList = gisLayerProductGroupMapper.selectGisLayerProductGroupList(groupQuery);
        List<GisLayerProductGroup> resultList = this.selectDieDaiList(groupList,userId);
        return resultList;
    }

    //循环当前组信息，进行组向下查询(迭代)
    private List<GisLayerProductGroup> selectDieDaiList(List<GisLayerProductGroup> groupList,Long userId){
        List<GisLayerProductGroup> resultList = new ArrayList<>();
        if(groupList!=null&&groupList.size()>0){
            groupList.stream().forEach(item->{
                //当前组下的图层信息
                GisLayerProduct gisLayerProductQuery = new GisLayerProduct();
                gisLayerProductQuery.setUserId(userId);
                gisLayerProductQuery.setGroupId(item.getId());
                List<GisLayerProduct> list = gisLayerProductMapper.selectGisLayerProductList(gisLayerProductQuery);
                item.setLayerProducts(list);

                //当前组下的组信息
                GisLayerProductGroup groupQuery = new GisLayerProductGroup();
                groupQuery.setUserId(userId);
                groupQuery.setGroupPid(item.getId());
                List<GisLayerProductGroup> groupTempList = gisLayerProductGroupMapper.selectGisLayerProductGroupList(groupQuery);
                List<GisLayerProductGroup> groups = this.selectDieDaiList(groupTempList,userId);
                item.setLayerProductGroups(groups);

            });
            resultList.addAll(groupList);
        }
        return resultList;
    }
}
